#!/bin/bash

# 动态gRPC客户端管理器测试脚本 (Linux版本)
# 用于测试根据serverId区分不同channel的功能

echo "🧪 动态gRPC客户端管理器测试脚本"
echo "=================================="

echo "📋 测试说明:"
echo "  本脚本将测试DynamicGrpcClientManager的功能"
echo "  验证是否能够根据serverId创建和管理不同的gRPC连接"
echo

echo "🚀 开始测试..."
echo

echo "1. 启动多个服务实例进行测试"
echo

# 启动多个Gate服务实例
echo "启动Gate服务实例1 (serverId=1001, gRPC端口=14000, Web端口=4000)"
cd gate && nohup mvn spring-boot:run -Dspring-boot.run.jvmArguments="-Dserver.id=1001 -Dgrpc.server.port=14000 -Dserver.port=4000" > ../gate-1001.log 2>&1 &
GATE1_PID=$!
cd ..

sleep 10

echo "启动Gate服务实例2 (serverId=1002, gRPC端口=14001, Web端口=4001)"
cd gate && nohup mvn spring-boot:run -Dspring-boot.run.jvmArguments="-Dserver.id=1002 -Dgrpc.server.port=14001 -Dserver.port=4001" > ../gate-1002.log 2>&1 &
GATE2_PID=$!
cd ..

sleep 10

echo "启动Lobby服务实例1 (serverId=2001, gRPC端口=15000, Web端口=5000)"
cd lobby && nohup mvn spring-boot:run -Dspring-boot.run.jvmArguments="-Dserver.id=2001 -Dgrpc.server.port=15000 -Dserver.port=5000" > ../lobby-2001.log 2>&1 &
LOBBY1_PID=$!
cd ..

sleep 10

echo "启动Lobby服务实例2 (serverId=2002, gRPC端口=15001, Web端口=5001)"
cd lobby && nohup mvn spring-boot:run -Dspring-boot.run.jvmArguments="-Dserver.id=2002 -Dgrpc.server.port=15001 -Dserver.port=5001" > ../lobby-2002.log 2>&1 &
LOBBY2_PID=$!
cd ..

sleep 10

echo
echo "2. 等待服务启动完成..."
echo

sleep 30

echo
echo "3. 验证服务注册状态"
echo

echo "检查Gate服务实例1:"
curl -s http://localhost:4000/actuator/health
echo

echo "检查Gate服务实例2:"
curl -s http://localhost:4001/actuator/health
echo

echo "检查Lobby服务实例1:"
curl -s http://localhost:5000/actuator/health
echo

echo "检查Lobby服务实例2:"
curl -s http://localhost:5001/actuator/health
echo

echo
echo "4. 测试gRPC连接"
echo

echo "测试Gate服务实例1的gRPC连接 (serverId=1001):"
grpcurl -plaintext localhost:14000 list
echo

echo "测试Gate服务实例2的gRPC连接 (serverId=1002):"
grpcurl -plaintext localhost:14001 list
echo

echo "测试Lobby服务实例1的gRPC连接 (serverId=2001):"
grpcurl -plaintext localhost:15000 list
echo

echo "测试Lobby服务实例2的gRPC连接 (serverId=2002):"
grpcurl -plaintext localhost:15001 list
echo

echo
echo "5. 查看服务日志"
echo

echo "Gate服务实例1日志 (serverId=1001):"
tail -n 20 gate-1001.log
echo

echo "Gate服务实例2日志 (serverId=1002):"
tail -n 20 gate-1002.log
echo

echo "Lobby服务实例1日志 (serverId=2001):"
tail -n 20 lobby-2001.log
echo

echo "Lobby服务实例2日志 (serverId=2002):"
tail -n 20 lobby-2002.log
echo

echo
echo "6. 验证Nacos服务发现"
echo

echo "访问Nacos控制台查看服务注册情况:"
echo "http://localhost:8848/nacos"
echo "用户名: nacos"
echo "密码: nacos"
echo

echo "7. 测试完成"
echo

echo "💡 测试要点:"
echo "  - 验证每个服务实例都有独立的gRPC端口"
echo "  - 验证DynamicGrpcClientManager能够根据serverId创建不同的连接"
echo "  - 验证服务发现能够正确识别不同的serverId"
echo "  - 验证负载均衡器能够根据serverId路由请求"
echo

echo "🎯 预期结果:"
echo "  - 每个服务实例都应该有独立的gRPC连接"
echo "  - 不同serverId的请求应该路由到对应的服务实例"
echo "  - 连接管理应该支持动态创建和销毁"
echo

echo "📊 进程信息:"
echo "Gate实例1 PID: $GATE1_PID"
echo "Gate实例2 PID: $GATE2_PID"
echo "Lobby实例1 PID: $LOBBY1_PID"
echo "Lobby实例2 PID: $LOBBY2_PID"
echo

echo "🔍 查看实时日志:"
echo "tail -f gate-1001.log"
echo "tail -f gate-1002.log"
echo "tail -f lobby-2001.log"
echo "tail -f lobby-2002.log"
echo

echo "🛑 停止所有服务:"
echo "kill $GATE1_PID $GATE2_PID $LOBBY1_PID $LOBBY2_PID"
echo 